package cn.bird.storm.validated.paramcheck.model.least;

import cn.bird.storm.validated.paramcheck.inter.ICheckBean;
import cn.bird.storm.validated.paramcheck.inter.ICheckModel;
import cn.hutool.core.collection.CollUtil;
import lombok.Data;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Data
public class LeastOneNotEmptyCheckModel implements ICheckModel<Object> {
    private List<ICheckBean<Object>> checkBeans;
    private String startStr = "";
    private String endStr = "不能同时为空！" + System.lineSeparator();

    @Override
    public String check() {
        Map<String, List<ICheckBean<Object>>> checkMap = getCheckBeans().stream()
                .collect(Collectors.groupingBy(vo -> ((LeastOneNotEmptyCheckBean) vo).getGroup()));
        if (CollUtil.isEmpty(checkMap)) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        checkMap.values()
                .forEach(beans -> sb.append(doGetMsg(beans)));
        return sb.toString();

    }

    /**
     * 对每组进行判断,不能全为空
     */
    private String doGetMsg(List<ICheckBean<Object>> notEmptyVOS) {
        if (CollUtil.isEmpty(notEmptyVOS)) {
            return "";
        }
        //如果有不需要提示的
        if (notEmptyVOS.stream()
                .filter(Objects::nonNull)
                .anyMatch(vo -> !vo.needPoint())) {
            return "";
        }
        return notEmptyVOS.stream().map(ICheckBean::getMsg)
                .collect(Collectors.joining(",")) + "不能同时为空！" + System.lineSeparator();
    }

}
